package com.linkage.jz.bankruptcy.activity.Vote;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.base.BaseActivity;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteDetail;
import com.linkage.jz.bankruptcy.fragment.Vote.ProVoteVotersDetailFragment_Manager;

import java.util.ArrayList;

import static com.linkage.jz.bankruptcy.fragment.Vote.ProVoteVotersDetailFragment_Manager.STATUS_WCH;
import static com.linkage.jz.bankruptcy.fragment.Vote.ProVoteVotersDetailFragment_Manager.STATUS_YBJ;
import static com.linkage.jz.bankruptcy.fragment.Vote.ProVoteVotersDetailFragment_Manager.STATUS_YCHWBJ;

/**
 * @author AFAP
 * 表决人详情 - 管理端
 */
public class ProVoteVotersDetailActivity_Manager extends BaseActivity {
    public static final String KEY_VOTE = "KEY_VOTE";

    private ProVoteDetail proVote;

    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;
    private ViewPager viewPager;
    private ArrayList<Fragment> items = new ArrayList<>();

    @Override
    public void initData() {
    }

    @Override
    public void initTitle() {
    }

    @Override
    public void initView() {
    }

    @Override
    public void setListener() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_pro_vote_voters_detail_manager);
        super.onCreate(savedInstanceState);

        proVote = (ProVoteDetail) getIntent().getSerializableExtra(KEY_VOTE);

        initViews();

        radioButton1.performClick();
    }


    public void initViews() {
        RadioGroup radioGroup = findViewById(R.id.radioGroup);
        radioButton1 = findViewById(R.id.radio1);
        radioButton2 = findViewById(R.id.radio2);
        radioButton3 = findViewById(R.id.radio3);

        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.radio1:
                    radioButton1.setTextSize(18);
                    radioButton2.setTextSize(16);
                    radioButton3.setTextSize(16);
                    viewPager.setCurrentItem(0);
                    break;
                case R.id.radio2:
                    radioButton1.setTextSize(16);
                    radioButton2.setTextSize(18);
                    radioButton2.setTextSize(16);
                    viewPager.setCurrentItem(1);
                    break;
                case R.id.radio3:
                    radioButton1.setTextSize(16);
                    radioButton2.setTextSize(16);
                    radioButton3.setTextSize(18);
                    viewPager.setCurrentItem(viewPager.getChildCount() - 1); //  这里不要写2，中间的tab不一定显示
                    break;
            }
        });

        FragmentManager fragmentManager = getSupportFragmentManager();
        // 参会未提交表决意见视为同意
        if (proVote.isDefaultAgreed()) {
            items.add(ProVoteVotersDetailFragment_Manager.newInstance(proVote, STATUS_YBJ));
            items.add(ProVoteVotersDetailFragment_Manager.newInstance(proVote, STATUS_WCH));
            findViewById(R.id.radio2).setVisibility(View.GONE);
        } else {
            items.add(ProVoteVotersDetailFragment_Manager.newInstance(proVote, STATUS_YBJ));
            items.add(ProVoteVotersDetailFragment_Manager.newInstance(proVote, STATUS_YCHWBJ));
            items.add(ProVoteVotersDetailFragment_Manager.newInstance(proVote, STATUS_WCH));
            findViewById(R.id.radio2).setVisibility(View.VISIBLE);
        }


        viewPager = findViewById(R.id.viewpager);
        ViewPagerAdapter adapter = new ViewPagerAdapter(fragmentManager, items);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0, false);
        viewPager.setOffscreenPageLimit(items.size());

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        radioButton1.setChecked(true);
                        break;
                    case 1:
                        if (proVote.isDefaultAgreed()) {
                            radioButton3.setChecked(true);
                        } else {
                            radioButton2.setChecked(true);
                        }
                        break;
                    case 2:
                        radioButton3.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private ArrayList<Fragment> items;

        ViewPagerAdapter(FragmentManager fm, ArrayList<Fragment> items) {
            super(fm);
            this.items = items;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return items.get(position);
                case 1:
                    return items.get(position);
                case 2:
                    return items.get(position);
            }
            return items.get(position);

        }

        @Override
        public int getCount() {
            return items.size();
        }
    }
}
